package com.share.hope.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.hope.entity.PostsInfo;
import com.share.hope.entity.UserLikesPosts;
import com.share.hope.entity.result.R;
import com.share.hope.mapper.LikesPostsMapper;
import com.share.hope.service.LikesPostsService;
import com.share.hope.service.PostsInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class LikesPostsServiceImpl extends ServiceImpl<LikesPostsMapper, UserLikesPosts> implements LikesPostsService {

    @Override
    public R<String> createLikes(Long userId, Long postsId) {
        try {
            this.save(UserLikesPosts.builder().userId(userId).postsId(postsId).build());
            return R.ok("点赞成功");
        }catch (Exception ex) {
            log.error("[createCollect] create collect poor_child fail, error msg is: {}", ex.getMessage());
            return R.error("点赞失败或已点赞");
        }
    }

    @Override
    public Map<Long, Long> getCount() {
        List<UserLikesPosts> lists = this.list();
        return lists.stream().collect(Collectors.groupingBy(UserLikesPosts::getPostsId, Collectors.counting()));
    }
}
